<template>
  <PageWrapper>
    <BasicTable title="品牌商" class="page-search-table" @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="openAdd">新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: openEdit.bind(null, record),
            },
            {
              label: '型号',
              onClick: openBtTable.bind(null, record),
            },
          ]"
          :dropDownActions="[
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              tooltip: '删除品牌商',
              popConfirm: {
                title: `确认删除品牌商:${record.name}吗？`,
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <Dialog @register="registerModel" @success="submitSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useModal } from '/@/components/Modal';
  import { tableConfig } from './tableData';
  import Dialog from './dialog.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { deleteBrandApi } from '/@/api/sys/brand/brand';
  import { DeleteModel } from '/@/extendModel/brandModel';

  export default defineComponent({
    components: {
      PageWrapper,
      BasicTable,
      Dialog,
      TableAction,
    },
    emits: ['select'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const [registerModel, { openModal: openModal }] = useModal();
      const [
        registerTable,
        { getSelectRowKeys, setSelectedRowKeys, clearSelectedRowKeys, reload },
      ] = useTable(tableConfig);
      function openAdd() {
        openModal(true, {
          isUpdate: false,
        });
      }
      function submitSuccess(e) {
        reload();
        createMessage.success(e.isUpdate ? '修改成功' : '添加成功');
      }
      function openEdit(rowdata: Recordable) {
        openModal(true, {
          rowdata,
          isUpdate: true,
        });
      }
      function openBtTable(rowdata: Recordable) {
        localStorage.setItem('brand_id', rowdata.id);
        emit('select', rowdata);
      }
      async function handleDelete(rowData: DeleteModel) {
        let id: any = rowData.id || null;
        await deleteBrandApi({
          brand_ids: [id],
        });
        createMessage.success('删除成功');
        reload();
      }
      return {
        registerModel,
        openModal,
        registerTable,
        submitSuccess,
        openBtTable,
        openAdd,
        openEdit,
        handleDelete,
      };
    },
  });
</script>
